﻿using CarRental.DDL;
using CarRental.Helpers;
using CarRental.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace CarRental.View.Account {
    public partial class Login : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
        }

        protected void btnLogin_Click(object sender, EventArgs e) {
            DbAccess _dbAccess = new DbAccess();
            List<user> _userLst = _dbAccess.GetUser().ToList<user>();

            string _loginName = tbUserName.Text;
            string _password = tbPassword.Text;

            var _user = _userLst.Where(w => w.login_name.ToUpper() == _loginName.Trim().ToUpper() && w.password == _password && w.status == "A").FirstOrDefault();
            if (_user != null) {
                Shared.LoginInfo.Authorized = true;
                Shared.LoginInfo.UserId = _user.id;
                Shared.LoginInfo.UserName = _user.user_name;
                Shared.LoginInfo.LoginName = _user.login_name;
                Shared.LoginInfo.UserType = _user.user_type;
                Shared.LoginInfo.UserEmail = _user.email;
                MessageBox.Show("Login Success", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Response.Redirect("~/View/Car/SearchCar.aspx");
            } else {
                lblMessage.Text = "* Login failed. Username or Password not correct.";
            }
        }
    }
}